/**
 * 
 */
package com.idea.auction.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;

import com.idea.auction.model.Admin;
import com.idea.auction.model.Dictionary;
import com.idea.auction.service.DictionaryService;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.server.Service;

/**
 * @author y.z
 */
@ControllerBind(controllerKey="/dic",viewPath="/pages")
public class DictionaryController extends BaseController {
	private static final Logger log = Logger.getLogger(DictionaryController.class);
	@Service
	private DictionaryService dictionaryService;
	
	/**
	 * 跳转到字典页
	 */
	@RequiresPermissions("dic/toDictionary")
	public void toDictionary(){
		getNavigation();
		render("dictionary/dictionary-list.html");
	}
	
	/**
	 * 跳转到子级字典页
	 */
	public void toChildDic(){
		Dictionary dictionary = dictionaryService.getById(this.getParaToInt("id"));
		setAttr("dicName", dictionary.get("dic_name"));
		setAttr("dicId", dictionary.get("id"));
		render("dictionary/dictionary-childre-list.html");
	}
	
	/**
	 * 跳转到子级添加字典页
	 */
	public void toChildEditDic(){
		Dictionary dictionary = new Dictionary();
		
		if(!StrKit.isBlank(getPara("id"))){
			dictionary = dictionaryService.getById(this.getParaToInt("id"));
		}
		
		setAttr("parentId",this.getPara("parentId"));
		setAttr("dictionary",dictionary );
		render("dictionary/dictionary-childre-edit.html");
	}
	
	/**
	 * 获取父级列表
	 */
	public void getDicList(){
		if(!StrKit.isBlank(getPara("curPage"))){
			curPage = getParaToInt("curPage");
		}
		super.setPage(dictionaryService.getPage(curPage));
	}
	
	/**
	 * 获取子级列表
	 */
	public void getChildreDicList(){
		if(!StrKit.isBlank(getPara("curPage"))){
			curPage = getParaToInt("curPage");
		}
		super.setPage(dictionaryService.getchildrePage(curPage,this.getParaToInt("id")));
	}
	
	/**
	 * 添加字典
	 */
	public void addDic(){
		Admin admin = (Admin) getSession().getAttribute("admin");
		Dictionary dictionary = new Dictionary();
		dictionary.set("dic_parent_id",this.getPara("dicParentId") );
		dictionary.set("dic_name", this.getPara("dicName"));
		dictionary.set("dic_code", this.getPara("dicCode"));
		dictionary.set("dic_value", this.getPara("dicValue"));
		dictionary.set("create_name", admin.get("name"));
		dictionary.set("create_date", new Date());
		dictionary.set("update_name", admin.get("name"));
		dictionary.set("update_date", new Date());
		
		if(dictionaryService.add(dictionary)){
			msgAdd();
		}
		
	}
	
	/**
	 * 编辑字典
	 */
	public void editDic(){
		Admin admin = (Admin) getSession().getAttribute("admin");
		Dictionary dictionary = new Dictionary();
		dictionary.set("id",this.getPara("id") );
		dictionary.set("dic_parent_id",this.getPara("dicParentId") );
		dictionary.set("dic_name", this.getPara("dicName"));
		dictionary.set("dic_code", this.getPara("dicCode"));
		dictionary.set("dic_value", this.getPara("dicValue"));
		dictionary.set("create_name", admin.get("name"));
		dictionary.set("create_date", new Date());
		dictionary.set("update_name", admin.get("name"));
		dictionary.set("update_date", new Date());
		
		if(dictionaryService.update(dictionary)){
			msgUpdate();
		}
		
	}
	
	/**
	 * 删除字典
	 */
	public void delDic(){
		String ids = getPara("ids");
		String idArray[] = ids.split(",");
		
		for(String id:idArray){
			dictionaryService.del(Integer.parseInt(id));
		}
		
		msgSuccess();
	}
	
	/**
	 * 验证标识
	 */
	public void validateCode(){
		
		if(dictionaryService.validateCode(this.getPara("dicCode"))){
			msgSuccess();
		}else{
			super.msgSetVal("该标识已存在");
		}
	}
	
	/**
	 * 验证字典的值
	 */
	public void validateValue(){
		
		if(dictionaryService.validateValue(this.getPara("value"), this.getPara("parentId"),this.getPara("id"))){
			msgSuccess();
		}else{
			super.msgSetVal("该值已存在");
		}
	}
	
	/**
	 * 根据code获取字典集
	 */
	public void getDicListByCode(){
		List<Dictionary> dictionary = dictionaryService.getListByCode(this.getPara("dicCode"));
		msgSuccess(dictionary);
	}
	
	/**
	 * 根据code与值获取显示名
	 */
	public void getDicValue(){
		msgSuccess(dictionaryService.getName(this.getPara("dicCode"), this.getPara("dicValue")));
	}
}
